<?php
/**
 * @software         [JiJinCMS]
 * @author           [上海集锦信息科技有限公司]
 * @website          [http://www.jijinweb.com]
 * @email            [sunny@jijinweb.com]
 **/
namespace app\admin\controller;
use think\facade\View;
use app\admin\model\RoleModel;
use think\facade\Db;
use think\facade\Request;
class AdCat extends \app\admin\controller\Admin{
    public function __construct(\think\App $app) {
        $this->model=new \app\admin\model\AdCatModel();
        parent::__construct($app);
    }    
    //
    public function index(){
        $data=$this->model->order("sort asc,catid asc")->select();
        View::assign("data",$data);
        return View::fetch();
    }
    //
    public function add(){
        if(request()->isPost()){
            $data=Request::param();
            $validate = \think\facade\Validate::rule(['name'=>'require'])->message([
                "name.require"=>"请填写分类名称",
            ]);
            if (!$validate->check(Request::param())) {
                $this->error($validate->getError());
            }
            $this->model->save($data);
            $lastId=$this->model->catid;
            //
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$lastId,$this->request->param());
            $this->success("添加成功",url('index'));
        }else{
            return View::fetch();
        }
    }
    public function update(){
        if(request()->isPost()){
            $data=Request::param();
            $validate = \think\facade\Validate::rule(['name'=>'require'])->message([
                "name.require"=>"请填写分类名称",
            ]);
            if (!$validate->check(Request::param())) {
                $this->error($validate->getError());
            }
            $this->model->update($data);
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$data['catid'],$this->request->param());
            $this->success("修改成功",url('index'));
         
        }else{
            $catid=Request::param("catid",0,"intval");
            $r=$this->model->find($catid);
            View::assign("r",$r);
            return View::fetch();
        }        
    }      
    //
    public function delete(){
        $catid=Request::param('catid');
        $this->model->destroy($catid);
        $this->success("删除成功");
    }
    //字段排序
    public function sort(){
        $ids=Request::param("catid");
        $sort=Request::param("sort");
        foreach($ids as $key=>$id){
            $data['catid']=(int)$id;
            $data['sort']=(int)$sort[$key];
            $this->model->update($data);
        }
        $this->success("排序更改成功");
    }     
}
